﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using ProjectVLIB_LAB04.KetNoiDB;
using System.Data.SqlClient;
using ProjectVLIB_LAB04.VOL;

namespace ProjectVLIB_LAB04.BLL
{
    class HoaDonBanBL
    {
         private DataAccess dataAccess;

        public HoaDonBanBL()
        {
            dataAccess = new DataAccess();
        }

        /// <summary>
        /// 1. Hiển thị danh sách hoa don ban
        /// </summary>
        /// <returns></returns>
        public DataTable GetData()
        {
            // cau query select
            string sql = "SELECT MaHD, MaKhach, NgayHD, DienGiai, Duyet, NgayDuyet FROM tbl_HoaDonBan ORDER BY MaHD DESC ";

            DataTable dt = new DataTable();
            // do toan bo du lieu vao datatable
            dt = dataAccess.GetData(sql);
            return dt;
        }

        /// <summary>
        /// 2. Thêm hoa don ban
        /// </summary>
        /// <param name="obj">HoaDonBan</param>
        /// <returns>true</returns>
        public bool InsertData(HoaDonBan obj)
        {
            SqlParameter[] param = new SqlParameter[5];
            param[0] = new SqlParameter("@MaKhach", SqlDbType.Int) { Value = obj.MaKhach };
            param[1] = new SqlParameter("@NgayHD", SqlDbType.DateTime) { Value = obj.NgayHD };
            param[2] = new SqlParameter("@DienGiai", SqlDbType.NVarChar) { Value = obj.DienGiai };
            param[3] = new SqlParameter("@Duyet", SqlDbType.Int) { Value = obj.Duyet };
            param[4] = new SqlParameter("@NgayDuyet", SqlDbType.DateTime) { Value = obj.NgayDuyet };

            // cau query select
            string sql = "INSERT INTO tbl_HoaDonBan  (MaKhach, NgayHD, DienGiai, Duyet, NgayDuyet) VALUES (@MaKhach, @NgayHD, @DienGiai,@Duyet,@NgayDuyet)";
            dataAccess.Execute(sql, param);

            return true;
        }

        /// <summary>
        /// 3. Xóa hoa don ban
        /// </summary>
        /// <param name="MaHD">MaHD INT</param>
        /// <returns>true</returns>
        public bool DeleteData(int MaHD)
        {
            SqlParameter[] param = new SqlParameter[1];
            param[0] = new SqlParameter("@MaHD", SqlDbType.Int) { Value = MaHD };
            // cau query select
            string sql = "DELETE FROM tbl_HoaDonBan WHERE MaHD = @MaHD";
            dataAccess.Execute(sql, param);

            return true;
        }

        /// <summary>
        /// 4. sửa hoa don ban
        /// </summary>
        /// <param name="obj">HoaDonBan</param>
        /// <returns>true</returns>
        public bool UpdateData(HoaDonBan obj)
        {
            SqlParameter[] param = new SqlParameter[6];
            param[0] = new SqlParameter("@MaKhach", SqlDbType.Int) { Value = obj.MaKhach };
            param[1] = new SqlParameter("@NgayHD", SqlDbType.DateTime) { Value = obj.NgayHD };
            param[2] = new SqlParameter("@DienGiai", SqlDbType.NVarChar) { Value = obj.DienGiai };
            param[3] = new SqlParameter("@Duyet", SqlDbType.Int) { Value = obj.Duyet };
            param[4] = new SqlParameter("@NgayDuyet", SqlDbType.DateTime) { Value = obj.NgayDuyet };
            param[5] = new SqlParameter("@MaHD", SqlDbType.Int) { Value = obj.MaHD };

            // cau query select
            string sql = "UPDATE tbl_HoaDonBan SET MaKhach = @MaKhach, NgayHD = @NgayHD, DienGiai = @DienGiai, Duyet = @Duyet, NgayDuyet = @NgayDuyet  " +
                           " WHERE MaHD = @MaHD";
            dataAccess.Execute(sql, param);

            return true;
        }

        /// <summary>
        /// 5. Tìm theo ma hoa don
        /// </summary>
        /// <param name="MaHD"></param>
        /// <returns></returns>
        public DataTable GetDataById(int MaHD)
        {
            SqlParameter[] param = new SqlParameter[1];
            param[0] = new SqlParameter("@MaHD", SqlDbType.Int) { Value = MaHD };
            // cau query select
            string sql = "SELECT * FROM tbl_HoaDonBan WHERE MaHD = @MaHD";
            DataTable dt = dataAccess.GetData(sql, param);

            return dt;
        }

        // 6. Login
        // 7. Logout
    }
    
}
